package ${model_package_serviceImpl};

import com.modules.utils.BeanUtils;
import com.models.constant.JafCoreConstant;
import ${model_package_dao}.${model_name}Dao;
import ${name};
import ${model_package_service}.${model_name}Service;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;



/**
* Created by lewis on ${.now}.
*/
@Service
@Transactional
public class ${model_name}ServiceImpl implements ${model_name}Service {
    private static Logger logger	= LoggerFactory.getLogger(${model_name}ServiceImpl.class);

   @Autowired
    private ${model_name}Dao ${model_name_entity}Dao;

    public Page<${model_name}> findAll(Pageable pageable){

        return this.${model_name_entity}Dao.findAll(pageable);
    }
    public Page<${model_name}> findAll(Specification<${model_name}> spec,PageRequest pageRequest){
        return this.${model_name_entity}Dao.findAll(spec,pageRequest);
    }
    
    @Transactional(readOnly = true)
	public ${model_name} get(${model_key_type} ${model_key}) {
		return this.${model_name_entity}Dao.findOne(${model_key});
	}
    
    @Transactional(readOnly = true)
	public List<${model_name}> findAll() {
		return this.${model_name_entity}Dao.findAll();
	}
    
    @Transactional(readOnly = true)
   	public List<${model_name}> findAll(Specification<${model_name}> spec) {
   		return this.${model_name_entity}Dao.findAll(spec);
   	}
    
    public List<${model_name}> findAll(Specification<${model_name}> spec, Sort sort) {
		return this.${model_name_entity}Dao.findAll(spec,sort);
	}
    
	public String save${model_name}(${model_name} ${model_name_entity}) {
		String result = null;
		String primaryKey = ${model_name_entity}.get${model_key_by}();
		${model_name} existed = null;
		if(StringUtils.isNotBlank(primaryKey)) {
			existed = get(primaryKey);
		}
		if (existed == null) {
			this.${model_name_entity}Dao.save(${model_name_entity});
			result = JafCoreConstant.SAVE_SUCCESS;
		}else {
			if ((existed.getVersion() != null) && (existed.getVersion().intValue() != ${model_name_entity}.getVersion().intValue())){
				result = JafCoreConstant.OUT_OF_DATE;//已经被别人编辑更新过了
			}else {
				String[] properties = new String[] {${model_properties}
						
				};
				BeanUtils.copyAssignProperties(${model_name_entity}, existed, properties);
				this.${model_name_entity}Dao.save(existed);
				result = JafCoreConstant.UPDATE_SUCCESS;
			}
		}
		return result;
	}
	
	public void delete${model_name}(${model_name} ${model_name_entity}) {
		this.${model_name_entity}Dao.delete(${model_name_entity});
		
	}

	
	public void delete${model_name}(${model_key_type} ${model_key}) {
		this.${model_name_entity}Dao.delete(${model_key});
	}
	
	
	
	

}
